﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FFS.BLL;
using FFS.Model;

namespace FFS
{
    public partial class frmFinanceItem : Form
    {
        public frmFinanceItem()
        {
            InitializeComponent();
        }

        private string actionType = "Add";
        FinanceItemInfo fii = new FinanceItemInfo();
        List<FinanceItemInfo> fii_list = new List<FinanceItemInfo>();
        FinanceItemService fiiService = new FinanceItemService();
        

        private void tsb_add_Click(object sender, EventArgs e)
        {
            actionType = "Add";
            panel_editFinanceItemInfo.Visible = !panel_editFinanceItemInfo.Visible;
            tsb_delete.Enabled = tsb_edit.Enabled = !panel_editFinanceItemInfo.Visible;
            txt_financeItemKey.Text = "";
            txt_financeItemName.Text = "";
            txt_financeItemRemark.Text = "";
            txt_financeItemKey.ReadOnly = false;
        }

        private void tsb_edit_Click(object sender, EventArgs e)
        {
            actionType = "Edit";
            if (listview_FinanceItemList.SelectedItems.Count == 1)
            {
                txt_financeItemKey.Text = listview_FinanceItemList.SelectedItems[0].SubItems[0].Text;
                txt_financeItemName.Text = listview_FinanceItemList.SelectedItems[0].SubItems[1].Text;
                txt_financeItemRemark.Text = listview_FinanceItemList.SelectedItems[0].SubItems[2].Text;
                txt_financeItemKey.ReadOnly = true;
                panel_editFinanceItemInfo.Visible = true;
                tsb_delete.Enabled = tsb_add.Enabled = tsb_edit.Enabled = !panel_editFinanceItemInfo.Visible;
            }
            else
            {
                MessageBox.Show("请选择要编辑的一条记录","提  示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void tsb_delete_Click(object sender, EventArgs e)
        {
            if (listview_FinanceItemList.SelectedItems.Count == 1)
            {
                if (MessageBox.Show("确定要删除 主键为[" + listview_FinanceItemList.SelectedItems[0].SubItems[0].Text.ToString().Trim() + "]的记录行数据吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    fiiService.ManageFinanceItem().DeleteFinanceItem(listview_FinanceItemList.SelectedItems[0].SubItems[0].Text.Trim());
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindListView(fiiService.ManageFinanceItem().GetAllFinanceItem());
                }
            }
            else 
            {
                MessageBox.Show("请选择要删除的记录行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);    
            }
        }

        private void tsb_search_Click(object sender, EventArgs e)
        {
            panel_search.Visible = !panel_search.Visible;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            panel_editFinanceItemInfo.Visible = false;
            tsb_add.Enabled = tsb_delete.Enabled = tsb_edit.Enabled = tsb_search.Enabled = true;
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (actionType == "Add")
            {
                if (txt_financeItemKey.Text.Trim() == "")
                {
                    MessageBox.Show("财务项目主键不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txt_financeItemKey.Focus();
                }
                else
                {
                    if (fiiService.ManageFinanceItem().IsExistsFinanceItem(txt_financeItemKey.Text.Trim()))
                    {
                        MessageBox.Show("财务项目主键(" + txt_financeItemKey.Text.Trim() + ")已经存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt_financeItemKey.SelectAll();
                    }
                    else
                    {
                        fii.financeItemKey = txt_financeItemKey.Text.Trim();
                        fii.financeItemName = txt_financeItemName.Text.Trim();
                        fii.financeItemRemark = txt_financeItemRemark.Text;
                        if (fiiService.ManageFinanceItem().CreateFinanceItem(fii) == 1)
                        {
                            MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txt_financeItemKey.Text = "";
                            txt_financeItemName.Text = "";
                            txt_financeItemRemark.Text = "";

                            btn_search_Click(sender,e);
                        }
                        else
                        {
                            MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else if (actionType == "Edit")
            {
                if (txt_financeItemKey.Text.Trim() == "")
                {
                    MessageBox.Show("财务项目主键不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    fii.financeItemKey = txt_financeItemKey.Text.Trim();
                    fii.financeItemName = txt_financeItemName.Text.Trim();
                    fii.financeItemRemark = txt_financeItemRemark.Text;
                    if (fiiService.ManageFinanceItem().ModifyFinanceItem(fii) == 1)
                    {
                        MessageBox.Show("编辑成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.panel_editFinanceItemInfo.Visible = false;
                        btn_search_Click(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("编辑失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            string searchCondition = " 1=1 ";
            if (cmb_searchItem.SelectedIndex == 0)
            {
                if (cmb_search_compare.SelectedIndex == 0)
                {
                    searchCondition = " FinanceItemKey like N'%" + txt_searchContent.Text.ToString().Trim() + "%'";
                }
                else
                {
                    searchCondition = " FinanceItemKey = N'" + txt_searchContent.Text.ToString().Trim() + "'";
                }
            }
            else if (cmb_searchItem.SelectedIndex == 1)
            {
                if (cmb_search_compare.SelectedIndex == 0)
                {
                    searchCondition = " FinanceItemName like N'%" + txt_searchContent.Text.ToString().Trim() + "%'";
                }
                else
                {
                    searchCondition = " FinanceItemName = N'" + txt_searchContent.Text.ToString().Trim() + "'";
                }
            }
            else
            {
                if (cmb_search_compare.SelectedIndex == 0)
                {
                    searchCondition = " FinanceItemRemark like N'%" + txt_searchContent.Text.ToString().Trim() + "%'";
                }
                else
                {
                    searchCondition = " FinanceItemRemark = N'" + txt_searchContent.Text.ToString().Trim() + "'";
                }
            }

            BindListView(fiiService.ManageFinanceItem().GetFinanceItemBySearchCondition(searchCondition));
        }

        private void FinanceItemManage_Load(object sender, EventArgs e)
        {
            cmb_search_compare.SelectedIndex = 0;
            cmb_searchItem.SelectedIndex = 0;
            BindListView(fiiService.ManageFinanceItem().GetAllFinanceItem());
        }


        public void BindListView(List<FinanceItemInfo> fiiList)
        {
            listview_FinanceItemList.Items.Clear();

            string[] listview_data = new string[5];

            foreach (FinanceItemInfo fi in fiiList)
            {
                if (fi != null)
                {
                    listview_data[0] = fi.financeItemKey.ToString().Trim();
                    listview_data[1] = fi.financeItemName.ToString().Trim();
                    listview_data[2] = fi.financeItemRemark;
                    listview_data[3] = fi.inputUser.ToString().Trim();
                    listview_data[4] = fi.inputDate.ToString().Trim();
                    ListViewItem item = new ListViewItem(listview_data);
                    listview_FinanceItemList.Items.Add(item);
                }
            }
        }

        private void tsm_add_Click(object sender, EventArgs e)
        {
            tsb_add_Click(sender,e);
        }

        private void tsm_edit_Click(object sender, EventArgs e)
        {
            tsb_edit_Click(sender,e);
        }

        private void tsm_delete_Click(object sender, EventArgs e)
        {
            tsb_delete_Click(sender,e);
        }

        private void tsm_search_Click(object sender, EventArgs e)
        {
            tsb_search_Click(sender,e);
        }

        private void listview_FinanceItemList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (actionType == "Edit")
                {
                    txt_financeItemKey.Text = listview_FinanceItemList.SelectedItems[0].SubItems[0].Text;
                    txt_financeItemName.Text = listview_FinanceItemList.SelectedItems[0].SubItems[1].Text;
                    txt_financeItemRemark.Text = listview_FinanceItemList.SelectedItems[0].SubItems[2].Text;
                }
            }
            catch (Exception)
            {
            }
        }


    }
}
